﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Web.UI.WebControls;

namespace mamnontuoitho7.AppCode
{
    public class Album
    {
        public ConnectGetData _connectData;
        public Album()
        {
            _connectData = new ConnectGetData();
        }
        public int AlbumCreateBySP(String ProductTypeID, String MemberID, String Title, String Introduce, String ProductStatusID, String ViewNumber, String PostUser, String AlbumPhoto)
        {
            int albumMaxID = -99;
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                SqlCommand command = new SqlCommand("sp_AlbumInsert", con);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ProductTypeID", SqlDbType.NVarChar).Value = ProductTypeID;
                command.Parameters.Add("@MemberID", SqlDbType.NVarChar).Value = MemberID;
                command.Parameters.Add("@Title", SqlDbType.NVarChar).Value = Title;
                command.Parameters.Add("@Introduce", SqlDbType.NVarChar).Value = Introduce;
                command.Parameters.Add("@ProductStatusID", SqlDbType.NVarChar).Value = ProductStatusID;
                command.Parameters.Add("@ViewNumber", SqlDbType.NVarChar).Value = ViewNumber;
                command.Parameters.Add("@PostUser", SqlDbType.NVarChar).Value = PostUser;
                command.Parameters.Add("@AlbumPhoto", SqlDbType.NVarChar).Value = AlbumPhoto;

                SqlParameter parameterReturnValue = new SqlParameter("productid", SqlDbType.Int, 4);
                parameterReturnValue.Direction = ParameterDirection.ReturnValue;
                command.Parameters.Add(parameterReturnValue);

                command.ExecuteNonQuery();
                albumMaxID = Int16.Parse(command.Parameters["productid"].Value.ToString());
                command.Dispose();
                con.Dispose();
                con.Close();
            }
            catch (SqlException ex)
            {
                Console.WriteLine("SQL Error" + ex.Message.ToString());

            }
            return albumMaxID;
        }
        /// <summary>
        /// create necessary folder for an album
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="locationpath"></param>
        public void CreateAlbumFolder(String productID, String locationpath)
        {
            // Specify a "currently active folder" 
            string folderAlbum = "album-" + productID;
            string newfolderAlbumPath = System.IO.Path.Combine(locationpath, folderAlbum);

            // Create the folder for album :  ../images/album/album-prodictID
            System.IO.Directory.CreateDirectory(newfolderAlbumPath);

            // Create the folder "photo" in album folder : ../images/album/album-prodictID/photo
            string newPhotoPath = System.IO.Path.Combine(newfolderAlbumPath, "photo");
            System.IO.Directory.CreateDirectory(newPhotoPath);

            // Create the folder "thumbnail" in album/photo folder : ../images/album/album-prodictID/photo/
            string newThumbnialPath = System.IO.Path.Combine(newPhotoPath, "thumbnail");
            System.IO.Directory.CreateDirectory(newThumbnialPath);
        }
        /// <summary>
        /// update topic thumbmail of a album
        /// </summary>
        /// <param name="photo"></param>
        /// <param name="productID"></param>
        public void UpdateAlbumThumbmail(String photo, String productID)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sql = "update Album set AlbumPhoto = @Photo where AlbumID = @ProductID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ProductID", productID);
                cmd.Parameters.AddWithValue("@Photo", photo);

                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);

            }
        }

        /// <summary>
        /// update album information except photo
        /// </summary>
        /// <param name="productID"></param>
        /// <param name="articleTypeID"></param>
        /// <param name="title"></param>
        /// <param name="introduce"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public bool AlbumUpdate(String productID, String title, String introduce, String editUser)
        {
            SqlConnection con = _connectData.GetConnect();
            con.Open();
            try
            {
                string sql = "update Product set Title = @Title, Introduce = @Introduce, [EditDate] = default, [EditUser] = @edituser where ProductID = @ProductID";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@ProductID", productID);
                cmd.Parameters.AddWithValue("@Title", title);
                cmd.Parameters.AddWithValue("@Introduce", introduce);
                cmd.Parameters.AddWithValue("@edituser", editUser);

                cmd.ExecuteNonQuery();
                cmd.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return false;
            }
            return true;
        }



        /// <summary>
        /// get all album for admin or user
        /// </summary>
        /// <returns>datatable</returns>
        public DataTable GetAlbum(String role, String amount)
        {
            string str = "";
            if (role.Equals("admin"))
            {
                if (amount == "0")
                {
                    str = "select  * from AlbumView ORDER BY ProductID DESC";
                }
                else
                {
                    str = "select top(" + amount + ") * from AlbumView ORDER BY ProductID DESC";
                }
            }
            else  
            {
                str = "select  * from AlbumView where ProductStatusID = 0 ORDER BY ProductID DESC";
            }
            return _connectData.GetDataTable(str);
        }

        /// <summary>
        /// get album by id for user, page AlbumPhoto.aspx in module
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public DataTable GetAlbumByID(String productID)
        {

            SqlConnection con = _connectData.GetConnect();
            string sql = "select * from AlbumView where ProductID = @ProductID and ProductStatusID=0";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@ProductID", productID);
            DataTable dt = _connectData.GetDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// get album by id for admin
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public DataTable GetAlbumByIDAdmin(String productID)
        {

            SqlConnection con = _connectData.GetConnect();
            string sql = "select * from AlbumView where ProductID = @ProductID";
            SqlCommand cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@ProductID", productID);
            DataTable dt = _connectData.GetDataTable(cmd);
            return dt;
        }

        /// <summary>
        /// get album,title, image by id for user, page AlbumPhotoDetails.aspx in module
        /// </summary>
        /// <param name="productID"></param>
        /// <returns></returns>
        public DataSet GetAlbumInformationImageByID(String productID)
        {
            //SqlConnection con = _connectData.GetConnect();
            //con.Open();
            string sql = "select * from AlbumView where ProductID = "+productID+" and ProductStatusID=0; select * from Image where AlbumID = "+productID;
            DataSet ds = _connectData.GetDataSet(sql);
          //  con.Close();
            return ds;
        }



        /// <summary>
        /// get records of a page
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="step"></param>
        /// <returns>datatable</returns>
        public DataTable GetAlbumPaged(int pageIndex, int step)
        {
            //int startRowIndex = pageIndex * step + 1;
            //int endRowIndex = startRowIndex + step -1;
            int startRowIndex = (pageIndex - 1) * step + 1;
            int endRowIndex = startRowIndex + step - 1;
            string sql = "SELECT * FROM (SELECT  ROW_NUMBER() OVER(ORDER BY ProductID DESC) AS number, * FROM AlbumView where ProductStatusID = 0 ) AS TempTable WHERE number BETWEEN @startRowIndex AND @endRowIndex";
            SqlConnection con = _connectData.GetConnect();
     //       con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);

            // cmd.Parameters.AddWithValue("@TableName", tableName);
            cmd.Parameters.AddWithValue("@startRowIndex", startRowIndex);
            cmd.Parameters.AddWithValue("@endRowIndex", endRowIndex);
            DataTable dt = _connectData.GetDataTable(cmd);
       //     con.Close();
            return dt;
        }

        /// <summary>
        /// get total number of album 
        /// </summary>
        /// <param name="articleTypeID"></param>
        /// <returns>int</returns>
        public global::System.Nullable<int> TotalNumberOfAlbum()
        {

            SqlConnection con = _connectData.GetConnect();

            con.Open();

            string sql = "SELECT COUNT(*)FROM AlbumView WHERE  ProductStatusID=0";
            SqlCommand cmd = new SqlCommand(sql, con);

            object returnValue;

            returnValue = cmd.ExecuteScalar();
            con.Close();
            if (((returnValue == null) || (returnValue.GetType() == typeof(global::System.DBNull))))
            {
                return new global::System.Nullable<int>();
            }
            else
            {
                return new global::System.Nullable<int>(((int)(returnValue)));
            }
        }

        /// <summary>
        /// get some album list from AlbumPhotoDetails.aspx ex : "Album khác"
        /// </summary>
        /// <returns></returns>
        public DataTable GetSomeAlbum(String amount, String productID)
        {
            SqlConnection con = _connectData.GetConnect();
   //         con.Open();
            try
            {
                string sql = "select top(@Number) * from AlbumView where ProductStatusID=0 and ProductID !=@ProductID ORDER BY ProductID DESC";
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@Number", Convert.ToInt16(amount));
                cmd.Parameters.AddWithValue("@ProductID", productID);

                DataTable dt = _connectData.GetDataTable(cmd);
   //             con.Close();
                return dt;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine("Co loiiii  : " + ex.StackTrace);
                return null;
            }

        }
    }
}